<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <!-- 默认插槽=》内容 -->
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="show = true">新增角色</el-button>
            </el-row>
            <!-- 表格=》渲染列表数据 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 默认插槽 -->
                <template #default="{ row }">
                  <!-- 获取当前行数据 -->
                  <!-- <p>{{ row }}</p> -->
                  <el-button size="small" type="success" @click="saveSett(row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row,1)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="query.page"
                :page-size="query.pagesize"
                :total="total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <h1>黑马137</h1>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色 -->
    <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible.sync="show" width="40%" @close="close">
      <!-- 表单 -->
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="addRole">确定</el-button>
      </span>
    </el-dialog>
    <AssigPerm ref="perm" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, updateRole, getRoleDetail } from '@/api/setting'
// 权限分配组件
import AssigPerm from './components/assign-perm.vue'
export default {
  components: {
    AssigPerm
  },
  data () {
    return {
      show: false,
      // 权限分配弹层
      showAssignDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      list: [],
      query: {
        page: 1,
        pagesize: 10
      },
      total: 0 // 数据总条数
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 分配权限
    saveSett (row) {
      this.showAssignDialog = true
      // console.log(row)
      this.$refs.perm.getRoleDetail(row.id)
    },
    // 编辑角色
    async editRole (currRole) {
      this.show = true
      // console.log(currRole)
      const { data } = await getRoleDetail(currRole.id)
      console.log(data)
      this.roleForm = data
    },
    // 新增角色
    async addRole () {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        this.query.page = 1
        this.getList()
        this.show = false
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹层
    close () {
      console.log('弹层关闭')
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 删除角色
    async delRole (id) {
      /**
       * 1. 用户确认后
       * 2. 调用接口删除
       * 3. 刷新列表
       */
      try {
        await this.$confirm('确定要删除吗？')
        await deleteRole(id)
        this.query.page = 1
        this.getList()
        this.$message.success('删除成功！')
      } catch (error) {
        console.log('点击取消')
      }
    },
    // 转换分页
    changePage (currPage) {
      // console.log(currPage)
      this.query.page = currPage
      this.getList()
    },
    async getList () {
      const { data } = await getRoleList(this.query)
      // console.log(res)
      this.list = data.rows
      this.total = data.total
    }

  }

}
</script>

<style>
</style>
